package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import henu.javaweb.bean.Student;
import henu.javaweb.bean.Teacher;
import henu.javaweb.dao.StudentDao;
import henu.javaweb.util.WebDbPool;

/**
 * Servlet implementation class AddStudentServlet
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddStudentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String sno = request.getParameter("sno");
		String sname = request.getParameter("sname");
		String pwd=request.getParameter("spass");
		PrintWriter out=response.getWriter();
		String tId = "111";
		String tFaculty="计算机与信息工程学院";
		String tMajor="软件工程";
		Teacher t=new Teacher();
		HttpSession session = request.getSession();
		if (session.getAttribute("teacher") != null) {
			t=(Teacher)session.getAttribute("teacher");
			tFaculty=t.getT_faculty();
			tMajor=t.getT_major();
			Student student=new Student();
			student.setS_no(sno);
			student.setS_name(sname);
			student.setS_password(pwd);
			student.setS_faculty(tFaculty);
			student.setS_major(tMajor);
			student.setT_id(t.getT_id());
			StudentDao sDao=new StudentDao();
			boolean b=sDao.addStudent(student);
			
			response.sendRedirect("jsp/teacher/addStudent.jsp");
		}

		String a = URLEncoder.encode("添加失败", "UTF-8"); 
		out.print("<script type=\'text/javascript\'>alert(decodeURIComponent('"+a+"'));history.go(-1);</script>");


	}

}
